<?php
namespace app\common\model;

use think\Model;

/**
 * 按照试卷分类统计学习信息
 * Class CoursePaperCateLearnStats
 * @package app\common\model
 */
class CoursePaperCateLearnStats extends Model
{
    /**
     * 数据表名
     * @var string
     */
    protected $name = 'course_paper_cate_learn_stats';

    /**
     * 自动写入时间戳
     * @var bool
     */
    protected $autoWriteTimestamp = true;
    
    /**
     * 更新用户试卷分类学习统计数据
     * @param int $uid 用户ID
     * @param int $categoryId 试卷分类ID
     * @param array $data 更新的数据
     * @return int|string 更新或插入的结果
     */
    public function updateUserCateStats($uid, $categoryId, $data)
    {
        // 查询条件
        $where = [
            'uid' => $uid,
            'category_id' => $categoryId
        ];
        
        // 获取现有记录ID
        $id = $this->where($where)->value('id');
        
        // 确保数据中包含uid和category_id
        $data['uid'] = $uid;
        $data['category_id'] = $categoryId;
        
        if ($id) {
            // 更新现有记录
            return $this->where('id', $id)->update($data);
        } else {
            // 新增记录
            return $this->insert($data);
        }
    }
    
    /**
     * 根据分类ID设置用户试卷分类学习统计数据
     * @param int $uid 用户ID
     * @param int $categoryId 试卷分类ID
     * @return array|null 用户分类统计数据
     */
    public function setUserCateStatsByCategoryId($uid, $categoryId)
    {
        // 查询分类信息
        $cateModel = new CourseQuestionCategory();
        $cateInfo = $cateModel->where('id', $categoryId)->find();
        if (!$cateInfo) {
            return;
        }

        // 统计分类下的所有试卷统计信息直接汇总
        $paperModel = new CoursePaper();
        $paperIds = $paperModel->where('category_id', $categoryId)->column('id');
        if (($paperIds)) {
            $paperStatsModel = new CoursePaperLearnStats();
            $paperStats = $paperStatsModel->where([
                'uid' => $uid,
                ['paper_id', 'in', $paperIds]
            ])->field([
                'SUM(total_question_count) as total_question_count',
                'SUM(total_correct_count) as total_correct_count'
            ])->find();
            $data = [
                'total_question_count' => $paperStats['total_question_count'] ?? 0,
                'total_correct_count' => $paperStats['total_correct_count'] ?? 0,
                'correct_rate' => round(($paperStats['total_correct_count'] / $paperStats['total_question_count']) * 100, 2)
            ];
            $this->updateUserCateStats($uid, $categoryId, $data);
        }

        // 统计分类下面所有子分类的统计信息直接汇总
        $childCateIds = $cateModel->getAllChildIds($categoryId);
        if ($childCateIds) {
            $paperStats = $this->where([
                'uid' => $uid,
                ['category_id', 'in', $childCateIds]
            ])->field([
                'SUM(total_question_count) as total_question_count',
                'SUM(total_correct_count) as total_correct_count'
            ])->find();
            $data = [
                'total_question_count' => $paperStats['total_question_count'] ?? 0,
                'total_correct_count' => $paperStats['total_correct_count'] ?? 0,
                'correct_rate' => round(($paperStats['total_correct_count'] / $paperStats['total_question_count']) * 100, 2)
            ];
            $this->updateUserCateStats($uid, $categoryId, $data);
        }

         // 如果分类存在上级分类,则递归
         if ($cateInfo['pid'] > 0) {
            $this->setUserCateStatsByCategoryId($uid, $cateInfo['pid']);
         }
    }
} 